﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Dataglot;

namespace SampleApplication
{
    public partial class ConnectionStrings : System.Web.UI.Page
    {
        private class ConStringPlus 
        {
            public string Name { get; set; }
            public bool CanConnect { get; set; }
            public bool CanCreateProvider { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<ConStringPlus> strings = new List<ConStringPlus>(ConfigurationManager.ConnectionStrings.Count);
                ConnectionStringsDisplay.DataSource = ConfigurationManager.ConnectionStrings;
                ConnectionStringsDisplay.DataBind();
                foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
                {
                    DataFactory factory = new DataFactory();//connectionString.Name
                    DbConnection connection = factory.CreateConnection();
                    bool canConnect;
                    try
                    {
                        if(string.IsNullOrEmpty(connectionString.ConnectionString))
                        {
                            canConnect = false;
                        }
                        else
                        {
                            //connection.ConnectionTimeout = 1;
                            connection.Open();
                            canConnect = true;   
                        }
                        
                    }
                    catch(Exception ex)
                    {
                        if (ex.Message.Contains("aspnetdb"))
                        {
                            canConnect = false;
                        }
                        else if(ex.Message.Contains("No process is on the other end of the pipe"))
                            canConnect = false;
                        else
                            throw;
                        //Go on.
                        //
                    }
                    finally
                    {
                        if(connection!=null)
                        {
                            connection.Dispose();
                        }
                    }
                    
                    strings.Add(
                        new ConStringPlus()
                            {
                                Name =  connectionString.Name,
                                CanConnect =   canConnect
                            }
                        );
                }//foreach
                ConnectionStringTests.DataSource = strings;
                ConnectionStringTests.DataBind();
            }

            
        }
        protected override void OnInit(EventArgs e)
        {
            this.SampleCatTable.DataBound += new EventHandler(SampleCatTable_DataBound);
            base.OnInit(e);
        }

        void SampleCatTable_DataBound(object sender, EventArgs e)
        {
            InsertForm.Visible = SampleCatTable.Rows.Count == 0;
        }
    }
}